package com.xinsoft.controller.wms;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.ScmProduceConsumingDTO;
import com.xinsoft.entity.dto.ScmProduceConsumingParam;
import com.xinsoft.entity.vo.ScmProduceConsumingPageVo;
import com.xinsoft.entity.vo.ScmProduceConsumingVo;
import com.xinsoft.service.ScmProduceConsumingService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生产领用
 * @folder 云供应链/仓库管理/生产领用
 */
@RestController
@RequestMapping("/scm/produceConsuming")
public class ScmProduceConsumingController {
    @Autowired
    private ScmProduceConsumingService produceConsumingService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmProduceConsumingParam param) {
        return Response.succeed(produceConsumingService.findPageListByParam(param));
    }


    /**
     * 暂存
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmProduceConsumingDTO dto) {
        // 参数校验
        ValidatorUtils.validateEntity(dto);
        produceConsumingService.tempSave(dto);
        return Response.succeed();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<ScmProduceConsumingVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        return Response.succeed(produceConsumingService.detail(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        ids = ids.stream().distinct().collect(Collectors.toList());
        produceConsumingService.del(ids);
        return Response.succeed();
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") ScmProduceConsumingParam param, HttpServletResponse response) throws Exception {
        List<ScmProduceConsumingPageVo> list = produceConsumingService.findListByParam(param);
        ExcelUtils.exportByEasypoi(list, ScmProduceConsumingPageVo.class, "生产领用", "生产领用", response, "生产领用");
        return Response.succeed();
    }

    /**
     * 审核
     */
    @GetMapping("/audit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> audit(@ApiParam(value = "生产领用id",required = true) @RequestParam("id") Integer id){
        produceConsumingService.audit(id);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @GetMapping("/reverseAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> reverseAudit(@ApiParam(value = "生产领用id",required = true) @RequestParam("id") Integer id){
        produceConsumingService.reverseAudit(id);
        return Response.succeed();
    }

    /**
     * 保存并审核
     */
    @PostMapping("/saveAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> saveAudit(@RequestBody ScmProduceConsumingDTO dto){
        ValidatorUtils.validateEntity(dto);
        produceConsumingService.saveAudit(dto);
        return Response.succeed();
    }

}
